<?php
/**
 * Plugin Name: Kodify For Wordpress
 * Plugin URI: http://code.google.com/p/wp-kodify/
 * Description: An implementation of Kodify (a code highlighter by Chris Corbyn) in Wordpress
 * Version: 1.0
 * Author: Luke Visinoni
 * Author URI: http://lukevisinoni.com/
 * 
 * Kodify is a code highlighter written by Chris Corbyn (http://chriscorbyn.co.uk/). It is
 * written in Javascript and gracefully degrades to show just plain code if Javascript is not
 * available in the client. More coming soon...
 */

/**
 * Function: lv_code_mask
 * Arguments:
 * 		text - the html content with <code></code> blocks which need to be masked
 * Description:
 * Mask all <code></code> blocks so as to avoid any Wordpress auto-formatting. These blocks 
 * will be un-masked before being output back to the page.
 */
function lv_code_mask($text) {

	// initialize output string
	$output = "";
	// grab code tags and any content in between them
	$codeContent = preg_split("#(<code[^>]*>.*<\\/code>)#Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
	pr($codeContent);
	// loop over code and content and mask code
	foreach ($codeContent as $key => $content) {
		// if it's a code element
		if (preg_match("#^(<code[^>]*>.*<\\/code>)#Us", $content, $code)) {
			// mask code blocks and content
			$content = '[code]' . base64_encode($code[1]) . '[/code]';
		}
		// add to output regardless of if its a code block or not
		$output .= $content;
	}
	return $output;

}